﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Linq;
using System.Text;
using JKF.BusinessLayer.BLL;
using JKF.DataModule;
using JKF.DataModule.SqlServerDAL;
using JKF.Entities.Model;
using JKF.Substructure;


namespace JKF.BusinessLayer.BLL
{
   
    public partial class bllSYS_MODULE
    {
        /// <summary>
        /// 根据当前账户角色来获取模块列表
        /// </summary>
        /// <returns></returns>
        public List<SYS_MODULEinfo> GetNowUser_Role_Module()
        {
            string condition= string.Empty;
            Guid role_key = new Guid();
            if (bllSYS_USER.CurrentUser.ROLE_KEY != role_key)
            {
                // 根据当前角色来显示模块树
                condition = string.Format(@"module_key  in( 
select module_key from dbo.SYS_ROLE_MODULE where role_key = '{0}'
) order by module_code", bllSYS_USER.CurrentUser.ROLE_KEY.ToString());
            }
            return this.GetModelList(condition);
        }
        /// <summary>
        /// 得到模块列表
        /// </summary>
        /// <param name="MODULE_NAME"></param>
        /// <param name="DATE_START"></param>
        /// <param name="DATE_END"></param>
        /// <param name="pageCurrent"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageCount"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public DataTable GetModules(string MODULE_NAME, string DATE_START, string DATE_END,
    int pageCurrent, int pageSize, ref int pageCount, ref int rowCount)
        {
            dalSYS_MODULE sysModule = new dalSYS_MODULE();
            return dalSYS_MODULE.GetModules(MODULE_NAME, DATE_START, DATE_END, pageCurrent, pageSize, ref pageCount, ref rowCount);
        }

        public DataRow GetModuleInfo(string moduleKey)
        {
            var strSql = @"SELECT [MODULE_KEY]
      ,[MODULE_CODE]
      ,[MODULE_NAME]
      ,[MODULE_NAME_EN]
      ,[URL]
      ,[URL_PIC_SMALL]
      ,[URL_PIC_LARGE]
      ,[DISPLAY_ORDER]
      ,[REMARK]
      ,[DATE_CREATE]
      ,[DATE_UPDATE]
  FROM [KYJ_CUSTOMER].[dbo].[SYS_MODULE]
WHERE MODULE_KEY=@MODULE_KEY";
            var dbClass = DB.HR;
            var cmd = dbClass.GetSqlStringCommand(strSql);
            dbClass.AddInParameter(cmd, "MODULE_KEY", DbType.Guid, moduleKey.ToGuid());
            var dt = dbClass.ExecuteDataSet(cmd).Tables[0];
            return (dt != null && dt.Rows.Count > 0) ? dt.Rows[0] : null;
        }

        /// <summary>
        /// 添加或编辑模块
        /// </summary>
        /// <param name="moduleInfo"></param>
        /// <returns></returns>
        public string  EditOrInsertSysModule(SYS_MODULEinfo moduleInfo)
        {
            var dal = new dalSYS_MODULE();

            if (moduleInfo.MODULE_KEY.IsGuidB())
            {
                var tmodule = dal.GetModel(moduleInfo.MODULE_KEY);
                if (tmodule == null) throw new Exception("数据不存在.");

                moduleInfo.MODULE_KEY = tmodule.MODULE_KEY;
                moduleInfo.DATE_UPDATE = DateTime.Now;
                dal.Update(moduleInfo);
                dal = null;
            }
            else
            {
                moduleInfo.MODULE_KEY = Guid.NewGuid().ToString();
                moduleInfo.DATE_CREATE = DateTime.Now;
                moduleInfo.DATE_UPDATE = DateTime.Now;
                dal.Add(moduleInfo);
                dal = null;         
            }
            return moduleInfo.MODULE_KEY.ToString();
        }

        /// <summary>
        /// 删除模块
        /// </summary>
        /// <param name="moduleKey"></param>
        public void DeleteModule(string moduleKey)
        {
            var dbClass = DB.HR;
            dbClass.ExecuteNonQuery(CommandType.Text,string.Format("DELETE FROM SYS_MODULE WHERE MODULE_KEY='{0}' ", moduleKey));
            //dbClass.ExecuteNonQuerySql(string.Format("DELETE FROM SYS_MODULE WHERE MODULE_KEY='{0}' ", moduleKey), null);
            //dbClass.ExecuteNonQuerySql(string.Format("DELETE FROM SYS_MODULE WHERE MODULE_KEY='{0}' ",moduleKey),null);
        }


         /// <summary>
        /// 获取全部模块
        /// </summary>
        /// <returns></returns>
        public  DataTable GetSysModuleBLL()
        {
            return dalSYS_MODULE.GetSysModuleDAL();
        }

         /// <summary>
        /// 获取模块表与操作表中的数据，组装成了树形结构
        /// </summary>
        /// <returns></returns>
        public  List<SysModule_SysActionTree> GetSysModule_SysActionBLL()
        {
            return this.DataTableToTreeList(dalSYS_MODULE.GetSysModule_SysActionDAL());
        }

        /// <summary>
        /// 自定义模块表与操作表类，以组装树形结构
        /// </summary>
        public class SysModule_SysActionTree
        {
            /// <summary>
            /// 模块表主键和操作表中的主键
            /// </summary>
            public string KEY { get; set; }
            /// <summary>
            ///  模块编码及操作表中的主键ID
            /// </summary>
            public string MODULE_CODE { get; set; }
            /// <summary>
            ///  名称
            /// </summary>
            public string NAME { get; set; }
            /// <summary>
            ///  父ID
            /// </summary>
            public string PID { get; set; }
        }

        /// <summary>
        /// 将返回的DataTable数据转成List形式
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private List<SysModule_SysActionTree> DataTableToTreeList(DataTable dt)
        {
            List<SysModule_SysActionTree> modelList = new List<SysModule_SysActionTree>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                SysModule_SysActionTree model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new SysModule_SysActionTree();
                    if (dt.Rows[n]["KEY"] != null && dt.Rows[n]["KEY"].ToString() != "")
                    {
                        model.KEY = dt.Rows[n]["KEY"].ToString();
                    }
                    if (dt.Rows[n]["MODULE_CODE"] != null && dt.Rows[n]["MODULE_CODE"].ToString() != "")
                    {
                        model.MODULE_CODE = dt.Rows[n]["MODULE_CODE"].ToString();
                    }
                    if (dt.Rows[n]["NAME"] != null && dt.Rows[n]["NAME"].ToString() != "")
                    {
                        model.NAME = dt.Rows[n]["NAME"].ToString();
                    }
                    if (dt.Rows[n]["PID"] != null && dt.Rows[n]["PID"].ToString() != "")
                    {
                        model.PID = dt.Rows[n]["PID"].ToString();
                    } 
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        public Suggests SuggestModule(string keyWord, int page)
        {
            var oSuggests = new Suggests();
            var currpage = page + 1;
            var pagesize = JKF.Substructure.Utils.GetPageSize;

            var sqlWhere = string.Format("  MODULE_NAME like '{0}%'  ", keyWord.ToUpper());

            var rowCount = 0;
            var pageCount = 0;
            var ds = DataPage.GetPageInfo("SYS_MODULE", "MODULE_KEY", currpage, pagesize, "module_name", "module_code", sqlWhere, ref pageCount, ref rowCount);

            oSuggests._pageCnt = pageCount;
            oSuggests.Items = new List<string>();
            oSuggests.Items_value = new List<string>();
            oSuggests.Data = ds;

            foreach (DataRow item in ds.Tables[0].Rows)
            {
                oSuggests.Items_value.Add(item["MODULE_NAME"].ToString());
                oSuggests.Items.Add(string.Format("{0}", item["MODULE_NAME"]));
            }
            return oSuggests;         
        }
    } 
}
